package ftp;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * User: Vladimir
 * Date: 30.06.12 Time: 18:23
 */
public class FTPHelper {

    public static final String PICTURES_FOLDER = "/extramobi.ru/www/published/publicdata/EXTRAMOBI/attachments/SC/products_pictures/";

    public static void main(String[] args) throws IOException, InterruptedException {
        String server = "u327448.ftp.masterhost.ru";
//
//        FtpClient client = new FtpClient();
//
//        client.openServer(server);
//        client.cd();
        FTPClient client = new FTPClient();
        client.connect(server);
        boolean login = client.login("u327448", "ment9am5ttl");

        if (login) {
            File sourceDir = new File("images/products_pictures");
            for (File sourceFile : sourceDir.listFiles()) {
                store(client, PICTURES_FOLDER + sourceFile.getName(), new FileInputStream(sourceFile));
                System.out.println("File '" + sourceFile + "' stored!");
                Thread.sleep(100);
            }
        }
        client.logout();
        client.disconnect();
    }

    public static void store(FTPClient client, String storeFile, FileInputStream sourceIS) throws IOException {
        boolean stored = client.storeFile(storeFile, sourceIS);
        if (!stored) {
            throw new IllegalStateException("Does not store: " + storeFile);
        }
        sourceIS.close();
    }
}
